package com.mars.admin.modules.post.service.impl;

import com.mars.admin.modules.base.service.impl.BaseServiceImpl;
import com.mars.admin.modules.post.entity.TopicFollow;
import com.mars.admin.modules.post.mapper.TopicFollowMapper;
import com.mars.admin.modules.post.service.ITopicFollowService;
import com.mars.admin.modules.post.service.ITopicService;
import com.mybatisflex.core.paginate.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 话题关注服务实现类
 *
 * @author Mars
 * @version 1.0
 * @date 2025-01-07
 */
@Slf4j
@Service
public class TopicFollowServiceImpl extends BaseServiceImpl<TopicFollow> implements ITopicFollowService {

    @Autowired
    private TopicFollowMapper topicFollowMapper;
    
    @Autowired
    private ITopicService topicService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean followTopic(Long userId, Long topicId) {
        try {
            // 检查是否已经关注
            if (isFollowing(userId, topicId)) {
                return false;
            }
            
            // 创建关注记录
            TopicFollow topicFollow = new TopicFollow();
            topicFollow.setUserId(userId);
            topicFollow.setTopicId(topicId);
            topicFollow.setCreateTime(LocalDateTime.now());
            
            boolean saved = save(topicFollow);
            if (saved) {
                // 更新话题关注数
                topicService.updateTopicStats(topicId, 0, 1);
            }
            
            return saved;
        } catch (Exception e) {
            log.error("关注话题失败，用户ID：{}，话题ID：{}", userId, topicId, e);
            throw e;
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean unfollowTopic(Long userId, Long topicId) {
        try {
            boolean deleted = topicFollowMapper.deleteByUserIdAndTopicId(userId, topicId) > 0;
            if (deleted) {
                // 更新话题关注数
                topicService.updateTopicStats(topicId, 0, -1);
            }
            return deleted;
        } catch (Exception e) {
            log.error("取消关注话题失败，用户ID：{}，话题ID：{}", userId, topicId, e);
            throw e;
        }
    }

    @Override
    public boolean isFollowing(Long userId, Long topicId) {
        try {
            return topicFollowMapper.isFollowing(userId, topicId);
        } catch (Exception e) {
            log.error("检查用户是否关注话题失败，用户ID：{}，话题ID：{}", userId, topicId, e);
            return false;
        }
    }

    @Override
    public Page<TopicFollow> getUserFollowedTopics(Long userId, Page<TopicFollow> page) {
        try {
            return topicFollowMapper.selectByUserId(userId, page);
        } catch (Exception e) {
            log.error("查询用户关注的话题列表失败，用户ID：{}", userId, e);
            throw e;
        }
    }

    @Override
    public Page<TopicFollow> getTopicFollowers(Long topicId, Page<TopicFollow> page) {
        try {
            return topicFollowMapper.selectByTopicId(topicId, page);
        } catch (Exception e) {
            log.error("查询话题的关注用户列表失败，话题ID：{}", topicId, e);
            throw e;
        }
    }

    @Override
    public List<Long> getUserFollowedTopicIds(Long userId) {
        try {
            return topicFollowMapper.selectTopicIdsByUserId(userId);
        } catch (Exception e) {
            log.error("查询用户关注的话题ID列表失败，用户ID：{}", userId, e);
            throw e;
        }
    }

    @Override
    public List<Long> getTopicFollowerIds(Long topicId) {
        try {
            return topicFollowMapper.selectUserIdsByTopicId(topicId);
        } catch (Exception e) {
            log.error("查询话题的关注用户ID列表失败，话题ID：{}", topicId, e);
            throw e;
        }
    }

    @Override
    public int countTopicFollowers(Long topicId) {
        try {
            return topicFollowMapper.countByTopicId(topicId);
        } catch (Exception e) {
            log.error("统计话题的关注数量失败，话题ID：{}", topicId, e);
            throw e;
        }
    }

    @Override
    public int countUserFollowedTopics(Long userId) {
        try {
            return topicFollowMapper.countByUserId(userId);
        } catch (Exception e) {
            log.error("统计用户关注的话题数量失败，用户ID：{}", userId, e);
            throw e;
        }
    }

    @Override
    public List<Long> getHotFollowedTopicIds(Integer limit) {
        try {
            return topicFollowMapper.selectHotFollowedTopicIds(limit);
        } catch (Exception e) {
            log.error("查询热门关注的话题ID列表失败", e);
            throw e;
        }
    }
} 